package com.lin.java.tool.file.service.chain;

import com.lin.java.tool.file.dao.po.SysFileConfigEntity;
import com.lin.java.tool.file.model.FileContext;
import com.lin.java.tool.file.model.FileDownload;
import com.lin.java.tool.file.model.FileRecord;
import com.lin.java.tool.general.enums.PlatformEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author: linjiang
 * @date: 2025/1/21
 */
@Slf4j
public abstract class AbstractFileChain implements FileChain {
    /**
     * 上传平台
     */
    protected String platform;

    /**
     * 代理对象
     */
    protected FileChain delegate;

    public AbstractFileChain(FileChain delegate, PlatformEnum platform) {
        this.delegate = delegate;
        this.platform = platform.name();
    }

    @Override
    public void doUpload(FileContext context, List<FileRecord> resp) throws Exception {
        SysFileConfigEntity fileConfig = getFileConfig(context);
        if (fileConfig != null) {
            FileRecord record = uploadOss(context.getFile(), fileConfig);
            if (record != null) {
                resp.add(record);
            }
        }

        if (delegate != null) {
            delegate.doUpload(context, resp);
        }
    }

    @Override
    public FileDownload doDownload(FileContext context) throws IOException {
        FileRecord fileRecord = getFileRecord(context);
        if (fileRecord != null) {
            SysFileConfigEntity fileConfig = getFileConfig(context);
            InputStream stream = downloadOss(fileRecord, fileConfig);
            if (stream != null) {
                FileDownload download = new FileDownload();
                download.setRecord(fileRecord);
                download.setInputStream(stream);
                return download;
            }
        }

        if (delegate != null) {
            return delegate.doDownload(context);
        }

        return null;
    }

    /**
     * 提取文件配置
     *
     * @param context
     * @return
     */
    private SysFileConfigEntity getFileConfig(FileContext context) {
        // 抽取文件配置
        Optional<SysFileConfigEntity> first = context.getConfigList().stream().filter(item -> item.getPlatform().equalsIgnoreCase(platform)).findFirst();
        if (!first.isPresent()) {
            return null;
        }
        return first.get();
    }

    /**
     * 获取文件记录
     *
     * @param context
     * @return
     */
    private FileRecord getFileRecord(FileContext context) {
        // 抽取文件配置
        Optional<FileRecord> first = context.getRecords().stream().filter(item -> item.getPlatform().equalsIgnoreCase(platform)).findFirst();
        if (!first.isPresent()) {
            return null;
        }
        return first.get();
    }


    /**
     * 上传到oss服务中
     *
     * @param file
     * @param fileConfig
     * @return
     */
    protected abstract FileRecord uploadOss(MultipartFile file, SysFileConfigEntity fileConfig) throws IOException;

    /**
     * 下载文件
     *
     * @param fileReord
     * @param fileConfig
     * @return
     * @throws IOException
     */
    protected abstract InputStream downloadOss(FileRecord fileReord, SysFileConfigEntity fileConfig) throws IOException;

    /**
     * 创建全路径
     *
     * @param root
     * @return
     */
    protected String fullPath(String root) {
        // 会按照当天的时间戳进行来管理文件
        String path = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/";
        return (root + "/" + path).replace("//", "/");
    }
}
